package com.docker.core.interceptor;

import com.docker.core.constant.ConfigConstant;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.PropKit;

/**
 * @author azzcsimp
 * @date 2018/7/16 11:28
 */
public class CORSInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {
        // 允许访问的客户端域名
        inv.getController().getResponse().addHeader("Access-Control-Allow-Origin", PropKit.get(ConfigConstant.CORS_ALLOWED_ORIGINS));
        // 允许访问的方法名
        inv.getController().getResponse().addHeader("Access-Control-Allow-Methods", PropKit.get(ConfigConstant.CORS_ALLOWED_METHODS));
        // 允许服务端访问的客户端请求头
        inv.getController().getResponse().addHeader("Access-Control-Allow-Headers", PropKit.get(ConfigConstant.CORS_ALLOWED_HEADERS));
        //预检验请求时间
        inv.getController().getResponse().addHeader("Access-Control-Max-Age", PropKit.get(ConfigConstant.CORS_MAX_AGE));
        inv.getController().getResponse().addHeader("Access-Control-Allow-Credentials", PropKit.get(ConfigConstant.CORS_ALLOW_CREDENTIALS));

        inv.invoke();

    }
}
